﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3.Models;

namespace MVC3.Controllers
{
    public class StoreManagerController : Controller
    {
        private MvcMusicStoreEntities storeDb = new MvcMusicStoreEntities();
        //
        // GET: /StoreManager/

        public ActionResult Index()
        {
            var albums = storeDb.Albums.Include("Genre").Include("Artist").ToList();
            return View(albums);
        }

        //
        // GET: /StoreManager/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            ViewBag.Genres = storeDb.Genres.OrderBy(g => g.Name).ToList();
            ViewBag.Artists = storeDb.Artists.OrderBy(a => a.Name).ToList();
            var album = new Album();
            return View(album);
        } 

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Album album)
        {
            if (ModelState.IsValid)
            {
                storeDb.Albums.Add(album);
                storeDb.SaveChanges();

                return RedirectToAction("Index");
            }

            ViewBag.Genres = storeDb.Genres.OrderBy(g => g.Name).ToList();
            ViewBag.Artists = storeDb.Artists.OrderBy(a => a.Name).ToList();

            return View(album);
        }
        
        //
        // GET: /StoreManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            ViewBag.Genres = storeDb.Genres.OrderBy(g => g.Name).ToList();
            ViewBag.Artists = storeDb.Artists.OrderBy(a => a.Name).ToList();
            
            var album = storeDb.Albums.Single(a => a.AlbumId == id); 

            return View(album);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var album = storeDb.Albums.Find(id);

            if(TryUpdateModel(album))
            {
                storeDb.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.Genres = storeDb.Genres.OrderBy(g => g.Name).ToList();
                ViewBag.Artists = storeDb.Artists.OrderBy(a => a.Name).ToList();

                return View(album);
            }
        }

        //
        // GET: /StoreManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            var album = storeDb.Albums.Find(id);
            return View(album);
        }

        //
        // POST: /StoreManager/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var album = storeDb.Albums.Find(id);

            storeDb.Albums.Remove(album);
            storeDb.SaveChanges();

            return View("Deleted");
        }
    }
}
